<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

class JFX_File
{
    protected $errors = array();
    // this is for end users and ensuring browser support
    private $validIconExtensions = array(
        'jpg',
        'jpeg',
        'gif',
        'png',
        'ico',
        'tif',
        'tiff',
        'bmp'
    );
    private $params = array(
        'type' => '',
        'title' => '',
        'icon_data' => '',
        'description' => '',
        'created_tstamp' => 0,
        'modified_tstamps' => array(),
        'module' => '',
        'method' => '',
        'url' => '',
        'module_minimum_version' => '',
        'module_maximum_version' => '',
        'core_minimum_version' => '',
        'core_maximum_version' => '',
        'theme_minimum_version' => '',
        'theme_maximum_version' => '',
        'content' => '',
        'auto_install' => false
    );

    public function setIconFile($filename){
        $ext = strtolower(end(explode('.', $filename)));
        if(!in_array((string) $ext, $this->validIconExtensions) || !is_readable($filename)){
            $this->addError('Invalid file');
            return false;
        }
        $this->set('icon_data', file_get_contents($filename));
    }

    public function getErrors($reset=true){
        $errors = $this->errors;
        if($reset) $this->resetErrors();
        return $errors;
    }

    public function resetErrors(){
        $this->errors = array();
    }

    protected function addError($msg){
        $this->errors[] = $msg;
    }

    public function set($k, $v=''){
        if(is_array($k)){
            foreach($k as $k2=>$v2){
                $this->set($k2, $v2);
            }
            return;
        }
        if(array_key_exists($k, $this->params)) $this->params[$k] = $v;
    }

    public function get($k){
        if(array_key_exists($k, $this->params)) return $this->params[$k];
    }

    public function getParams(){
        return $this->params;
    }

    public function load($filename){
        if(!file_exists($filename) || !is_readable($filename)){
        $this->addError('File does not exist');
            return false;
        }
        $fc = file_get_contents($filename);
        $fileObject = unserialize($fc);
        if(!is_object($fileObject) || false === ($fileObject instanceof JFX_File)){
            $this->addError('Invalid file type');
            return false;
        }
        // valid file!
        $params = $fileObject->getParams();
        $this->set($params);
        return true;
    }

    public function save($filename){
        $THEME = JFX::registry('JFX_Theme');
        $CORE = JFX::registry('JFX_Core');

        if($this->get('module')!=''){
            $moduleObj = JFX_Module::newInstance($this->get('module'));
            if(!is_object($moduleObj) || (false === ($moduleObj instanceof JFX_Module) && false === ($moduleObj instanceof JFX_Theme))){
                JFX::addError('File cannot be saved, module is not specified');
                return false;
            }
            if($this->get('method')!=''){
                if(!is_callable($this->get('method'), $moduleObj)){
                    JFX::addError('File cannot be saved, method is not specified');
                    return false;
                }
            }
        }

        if($this->get('module_minimum_version')==''){
            $this->set('module_minimum_version', $moduleObj->getVersion());
        }
        if($this->get('theme_minimum_version')==''){
            $this->set('theme_minimum_version', $THEME->getVersion());
        }
        if($this->get('core_minimum_version')==''){
            $this->set('core_minimum_version', $CORE->getVersion());
        }

        if($this->params['created_tstamp']==0) $this->params['created_tstamp'] = time();
        else $this->params['modified_tstamps'][] = time();

        $fc = serialize($this);
        if(file_exists($filename)) unlink($filename);
        $fh = fopen($filename, 'w');
        fwrite($fh, $fc);
        fclose($fh);

        return true;

    }


}